#include "ASConfirmScreen.h"
#include "ASScreenMgr.h"
#include "ASScreenShare.h"
#include "..\app\ASAppImgInfo.h"
#include "..\app\ASAppConst.h"

static as_confirm_screen* s_confirm_screen_p;
/*for config*/
static as_button_cb s_lsk_func;
static as_button_cb s_rsk_func;
static ASSTR s_content;
static ASSTR s_lsk_text;
static ASSTR s_rsk_text;

static void as_confirm_screen_create_confirm_content(as_screen* screen_p, as_rect area)
{
    as_confirm_screen* confirm_screen_p = (as_confirm_screen*)screen_p;
    ASUINT param[4];
    ASINT total_height;

    if(!confirm_screen_p)
        return;

    param[0] = (ASUINT)as_ucs2_from_gb2312(s_content);
    confirm_screen_p->content = (as_label*)screen_p->add_ctrl(screen_p, AS_CTRL_LABEL, area, 1, (void**)param);

    if(!confirm_screen_p->content)
        return;

    confirm_screen_p->content->set_bg_color((as_control*)confirm_screen_p->content, DATA_SHEET_BG_COLOR);
    confirm_screen_p->content->set_bg_frame_color((as_control*)confirm_screen_p->content, AS_COLOR_WHITE);
    confirm_screen_p->content->set_text_color((as_control*)confirm_screen_p->content, SOFTKEY_TEXT_COLOR);
    //adjust area
    total_height = confirm_screen_p->content->get_total_height((as_control*)confirm_screen_p->content);

    if(total_height <= area.h)
        confirm_screen_p->content->set_layout((as_control*)confirm_screen_p->content, AS_LABEL_TEXT_CENTER);
    else
    {
        area.h = total_height;
        area.y = SCR_H - BOTTOM_BAR_HEIGHT - area.h - 2;
        confirm_screen_p->content->base.change_area_hdlr((as_control*)confirm_screen_p->content, area);
        confirm_screen_p->content->set_text_offset((as_control*)confirm_screen_p->content, 3, 2);
    }
}

void as_confirm_screen_init(as_screen* screen_p)
{
    as_confirm_screen* confirm_screen_p = (as_confirm_screen*)screen_p;
    as_rect area;

    if(!confirm_screen_p)
        return;

    s_confirm_screen_p = confirm_screen_p;
    area.x = 0;
    area.w = (ASINT16)as_get_lcd_width();
    area.h = BOTTOM_BAR_HEIGHT;
    area.y = (ASINT16)as_get_lcd_height() - area.h;
    confirm_screen_p->bottom_bar_bg_p = as_screen_create_bar_img(screen_p, area, IMG_BOTTOM);
    area.w = TEXT_SK_W;

    if(s_lsk_text && s_lsk_func)
    {
        area.x = 0;
        confirm_screen_p->lbtn_p = as_screen_create_lsk(screen_p, area, s_lsk_text, s_lsk_func);
    }

    if(s_rsk_text && s_rsk_func)
    {
        area.x = SCR_W - TEXT_SK_W;
        confirm_screen_p->rbtn_p = as_screen_create_rsk(screen_p, area, s_rsk_text, s_rsk_func);
    }

    area.w = SCR_W - 4;
    area.x = 2;
    area.h = 30;
    area.y = (ASINT16)as_get_lcd_height() - BOTTOM_BAR_HEIGHT - area.h - 2;
    as_confirm_screen_create_confirm_content(screen_p, area);
}
void as_confirm_screen_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_show_confirm_screen(ASSTR content, ASSTR lsk_text, ASSTR rsk_text,
                            as_void_func lsk_func, as_void_func rsk_func)
{
    as_screen_manager* scr_mgr_p = as_get_screen_stack_instance();
    as_screen* scr_p;
    as_confirm_screen* confirm_screen_p;

    if(!content)
        return;

    s_content = content;
    s_lsk_text = lsk_text;
    s_rsk_text = rsk_text;
    s_lsk_func = lsk_func;
    s_rsk_func = rsk_func;
    scr_mgr_p->new_and_push_screen(AS_SCREEN_CONFIRM);
    scr_p = scr_mgr_p->get_cur_screen();
    confirm_screen_p = (as_confirm_screen*)scr_p;

    /*change left soft key*/
    if(confirm_screen_p->lbtn_p)
        confirm_screen_p->lbtn_p->add_keycode((as_control*)confirm_screen_p->lbtn_p, AS_KEY_OK);

    if(scr_p)
    {
        //scr_p->repaint_hdlr(scr_p);
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}

void as_show_exit(void)
{
    as_show_confirm_screen(TEXT_IF_EXIT, TEXT_OK, TEXT_CANCEL, as_exit_app, as_screen_go_back);
}
